# from Final_Code_Replication_Final/02_Analysis/00_Main_Text/Figure_02_Interactions.R

# Code for Figure 2: The Effect of Pretrial Incarceration on 2012 Turnout by Income and Race

rm(list = ls())

## ---------------------------------------
## Load Packages 
## ---------------------------------------

require('dotwhisker')
require('gridExtra')
require('AER')
require('ivpack')
require('dplyr')
require('data.table')

## ---------------------------------------
## Load Data and Functions
## ---------------------------------------
load("../data0812.RData")

directory <- "../functions/"
functions <- list.files(directory)  
loadfunctions <- sapply(functions, FUN = function(x)source(paste0(directory, x)))

## ---------------------------------------
# Construct Instrument
## ---------------------------------------
data0812 <- constructIV(data0812)

## ---------------------------------------
#  Select Last Case Before Election
## ---------------------------------------
data0812 <- lastCase(data0812)

## ---------------------------------------
## Two Stage Least Squares Specifications
## ---------------------------------------
data0812$race <- as.factor(data0812$race)
data0812 <- within(data0812, race <- relevel(race, ref = "Black"))

data0812$race2 <- as.factor(data0812$race2)
data0812 <- within(data0812, race2 <- relevel(race2, ref = "Black"))

time.controls <- "as.factor(court_time1) + as.factor(court_time2) + as.factor(court_dow) + as.factor(court_shift) + as.factor(totOGS2)"
case.controls <-   "as.factor(any_drug_2) +  as.factor(any_violent_2) + as.factor(fire_arms_2) +  as.factor(any_rob_2) + as.factor(any_dui_2) + as.factor(prior_offender_2)"
demo.controls <- "age_2012 + I(age_2012^2) + Female + as.factor(race) + vote2008 + as.factor(noteli08) + regis_before"

## ---------------------------------------
## By Race (right panel)
## ---------------------------------------

# Model 1 - race categories = Black, Other, Unknown, White
outc.1 <- "vote2012"
endo.1 <- "pti * as.factor(race)"
inst.1 <- "judgeiv * as.factor(race)"

form.1 <- formula(paste(outc.1, "~", endo.1, "+" , time.controls, "|", inst.1, "+", time.controls))
form.2 <- formula(paste(outc.1, "~", endo.1, "+" , time.controls, "+", demo.controls, "|", inst.1, "+", time.controls, "+", demo.controls))
form.3 <- formula(paste(outc.1, "~", endo.1, "+" , time.controls, "+", demo.controls, "+", case.controls, "|", inst.1, "+", time.controls, "+", demo.controls, "+", case.controls))

m1a3 <- ivreg(form.3, data = data0812)

c1 <- c(robust.se(m1a3)["pti", 1], robust.se(m1a3)["pti", 2]) ## Black
c2se <- vcov(m1a3, type = "HC1")[c("pti", "pti:as.factor(race)White"), c("pti", "pti:as.factor(race)White")]
c2 <- c(sum(robust.se(m1a3)[c("pti", "pti:as.factor(race)White"), "Estimate"]), 
        sqrt(c2se[1, 1] + c2se[2, 2] + 2 * c2se[1, 2])) ## White (All)

# Model 2 - race categories = Black, Other, Unknown, White Hispanic, White Anglo
outc.1 <- "vote2012"
endo.1 <- "pti * as.factor(race2)"
inst.1 <- "judgeiv * as.factor(race2)"

form.1 <- formula(paste(outc.1, "~", endo.1, "+" , time.controls, "|", inst.1, "+", time.controls))
form.2 <- formula(paste(outc.1, "~", endo.1, "+" , time.controls, "+", demo.controls, "|", inst.1, "+", time.controls, "+", demo.controls))
form.3 <- formula(paste(outc.1, "~", endo.1, "+" , time.controls, "+", demo.controls, "+", case.controls, "|", inst.1, "+", time.controls, "+", demo.controls, "+", case.controls))

m1a3 <- ivreg(form.3, data = data0812)

c3se <- vcov(m1a3, type = "HC1")[c("pti", "pti:as.factor(race2)WhiteH"), c("pti", "pti:as.factor(race2)WhiteH")]
c3 <- c(sum(robust.se(m1a3)[c("pti", "pti:as.factor(race2)WhiteH"), "Estimate"]), 
        sqrt(c3se[1, 1] + c3se[2, 2] + 2 * c3se[1, 2])) ## White (All)

c4se <- vcov(m1a3, type = "HC1")[c("pti", "pti:as.factor(race2)WhiteA"), c("pti", "pti:as.factor(race2)WhiteA")]
c4 <- c(sum(robust.se(m1a3)[c("pti", "pti:as.factor(race2)WhiteA"), "Estimate"]), 
        sqrt(c4se[1, 1] + c4se[2, 2] + 2 * c4se[1, 2])) ## White (All)

m1 <- data.frame(rbind(c1, c4, c3, c2))
colnames(m1) <- c("estimate", "std.error")
m1$term <- c("black", "white_a", "white_h", "white")
m1_df <- data.frame(as.matrix(m1)) 
m1_df$estimate <- as.numeric(as.character(m1_df$estimate))
m1_df$std.error <- as.numeric(as.character(m1_df$std.error))

brackets <- list(c("Black", "White Anglo", "White Hispanic", "White"))

g2 <- {dwplot(m1_df, 
              dot_args = list(size = 2.5), whisker_args = list(size = 0.75),  
              vline = geom_vline(xintercept = 0, colour = "grey60", linetype = 2)) %>% 
    relabel_predictors(c(black = "Black", white_a = "White Anglo", white_h = "White Hispanic", white = "White (All)")) +
    theme_classic() + xlab("The Effect of Pretrial Incarceration on Turnout") + ylab("") + xlim(-.25, .25)+
    ggtitle("Racially Disparate Impact") +
    theme(plot.title = element_text(face="bold"), legend.position = "none",
          axis.text.x  = element_text(size = 12),
          axis.text.y  = element_text(size = 12),
          text = element_text(size = 13))} 
 

## ---------------------------------------
## By Income (left panel)
## ---------------------------------------

outc.1 <- "vote2012"
endo.1 <- "pti * as.factor(quants_inc)"
inst.1 <- "judgeiv * as.factor(quants_inc)"

form.1 <- formula(paste(outc.1, "~", endo.1, "+" , time.controls, "|", inst.1, "+", time.controls))
form.2 <- formula(paste(outc.1, "~", endo.1, "+" , time.controls, "+", demo.controls, "|", inst.1, "+", time.controls, "+", demo.controls))
form.3 <- formula(paste(outc.1, "~", endo.1, "+" , time.controls, "+", demo.controls, "+", case.controls, "|", inst.1, "+", time.controls, "+", demo.controls, "+", case.controls))

m1a3 <- ivreg(form.3, data = data0812)

c1 <- c(robust.se(m1a3)["pti", 1], robust.se(m1a3)["pti", 2]) 
c2se <- vcov(m1a3, type = "HC1")[c("pti", "pti:as.factor(quants_inc)2"), c("pti", "pti:as.factor(quants_inc)2")]
c2 <- c(sum(robust.se(m1a3)[c("pti", "pti:as.factor(quants_inc)2"), "Estimate"]), 
        sqrt(c2se[1, 1] + c2se[2, 2] + 2 * c2se[1, 2])) 
c3se <- vcov(m1a3, type = "HC1")[c("pti", "pti:as.factor(quants_inc)3"), c("pti", "pti:as.factor(quants_inc)3")]
c3 <- c(sum(robust.se(m1a3)[c("pti", "pti:as.factor(quants_inc)3"), "Estimate"]), 
        sqrt(c3se[1, 1] + c3se[2, 2] + 2 * c3se[1, 2])) 

m1 <- data.frame(rbind(c1, c2, c3))
colnames(m1) <- c("estimate", "std.error")
m1$term <- c("first", "second", "third")
m1_df <- data.frame(as.matrix(m1)) 
m1_df$estimate <- as.numeric(as.character(m1_df$estimate))
m1_df$std.error <- as.numeric(as.character(m1_df$std.error))


g1 <- {dwplot(m1_df, 
              dot_args = list(size = 2.5), whisker_args = list(size = 0.75),  
              vline = geom_vline(xintercept = 0, colour = "grey60", linetype = 2)) %>% 
    relabel_predictors(c(first = "Bottom tercile", second = "Middle tercile", third = "Top tercile")) +
    theme_classic() + xlab("The Effect of Pretrial Incarceration on Turnout") + ylab("") + xlim(-.25, .25) +
    ggtitle("Resource Deprivation") +
    theme(plot.title = element_text(face="bold"), legend.position = "none",
          axis.text.x  = element_text(size = 12),
          axis.text.y  = element_text(size = 12),
          text = element_text(size = 13))}

## ---------------------------------------
# Plot Figure 2
## ---------------------------------------
pdf(file = "../output/Figures/Figure_02.pdf", width = 11, height = 4)
grid.arrange(g1, g2, ncol = 2)
dev.off()

cat("\nFigure 2: The Effect of Pretrial Incarceration on 2012 Turnout by Income and Race\n")
cat("Saved Figure 2 in /output/Figures/Figure_02.pdf\n\n")


